package com.mathdroid.core;

import java.io.File;
import java.util.LinkedList;

import com.mathdroid.model.Profile;
import com.mathdroid.model.Profiles;
import com.mathdroid.model.abilities.AbilityFactory;
import com.mathdroid.model.abilities.AbilityTemplate;
import com.mathdroid.model.abilities.AbilityTemplates;
import com.mathdroid.model.buffs.BuffFactory;
import com.mathdroid.model.buffs.BuffTemplates;
import com.mathdroid.model.encounters.EncounterFactory;
import com.mathdroid.model.encounters.EncounterTemplates;
import com.mathdroid.model.enums.ESpec;
import com.mathdroid.model.talents.Spec;
import com.mathdroid.model.talents.Specs;
import com.mathdroid.model.talents.SpecFactory;
import com.mathdroid.persistence.Serializer;

/**
 * This class checks consistency of folders on startup and creates the correct
 * folders, if necessary. Also the data files are checked and maybe default
 * files loaded.
 */
public class Setup {

	/**
	 * This list contains the files/folders to check.
	 */
	private LinkedList<File> folders = new LinkedList<File>();

	/**
	 * The file extension for data files.
	 */
	private static final String SUFFIX = Config.getProperty("suffix");

	/**
	 * Standard constructor.
	 */
	public Setup() {
		this.createFolders();
		this.createDefaultTemplates();
	}

	/**
	 * Create the default data files (templates), if necessary.
	 */
	private void createDefaultTemplates() {
		Serializer serializer = new Serializer();
		LinkedList<ESpec> spec = ESpec.getAllSpecs();
		
		File file = null;
		
		/* Check each "default specs" */
		for (ESpec s : spec) {
			file = new File("data/specs/" + s.toString()
					.toLowerCase() + Setup.SUFFIX);
			if (!file.exists()) {
				Specs specs = new Specs();
				Spec untalented = SpecFactory.getTalents(s);
				specs.put(untalented.getName(), untalented);
				serializer.serialize(specs, s.toString().toLowerCase());
			}
		}
		
		/* Check each "default abilities" */
		AbilityFactory af = new AbilityFactory();
		for (ESpec s : spec) {
			file = new File("data/abilities/" + s.toString()
					.toLowerCase() + Setup.SUFFIX);
			if (!file.exists()) {
				AbilityTemplates at = new AbilityTemplates();
				AbilityTemplate t = af.get(s);
				at.put(t.toString(), t);
				serializer.serialize(at, s.toString().toLowerCase());
			}
		}

		/* Check "profiles" */
		file = new File("data/profiles" + Setup.SUFFIX);
		if (!file.exists()) {
			Profiles profiles = new Profiles();

			/* Create default profiles for each spec */
			for (ESpec s : spec) {
				Profile profile = new Profile("Default " + s, s);
				profile.setSpecTemplate(SpecFactory.getTalents(s));
				profiles.put(profile.getName(), profile);
			}

			serializer.serialize(profiles);
		}
		
		/* Check "buffs" */
		file = new File("data/buffs" + Setup.SUFFIX);
		if (!file.exists()) {
			BuffTemplates t = new BuffTemplates();
			t.put("Unbuffed", BuffFactory.getNewUnbuffedTemplate());
			t.put("Fully buffed", BuffFactory.getNewFullyBuffedTemplate());
			serializer.serialize(t);
		}
		
		/* Check "encounters" */
		file = new File("data/encounters" + Setup.SUFFIX);
		if (!file.exists()) {
			EncounterTemplates t = EncounterFactory.getDefaultTemplates();
			serializer.serialize(t);
		}
	}

	/**
	 * Create the folders, if missing.
	 */
	private void createFolders() {
		this.folders.add(new File("data"));
		this.folders.add(new File("data/specs"));
		this.folders.add(new File("data/abilities"));

		for (File f : this.folders) {
			if (!f.exists()) {
				f.mkdirs();
			}
		}
	}
}
